class Scene2 extends Scene {
	public constructor() {
		super();
	}
	private NowTIME;
	private jisiqi;
	private GJname;
	private shuju;
	private bt2;
	private gg;
	private gg1;
	private gg2;
	private Text1;
	private suiji;
	protected async onadd() {
		SoundManager.Instance.playbg();
		//遍历图片,将场景铺满
		// for (var i = 0; i < 12; i++) {
		// 	for (var j = 0; j < 21; j++) {
		// 		let bt1: egret.Bitmap = new egret.Bitmap();
		// 		bt1.texture = RES.getRes('GameAssets_json.bgTile');
		// 		bt1.x = i * 40;
		// 		bt1.y = j * 38;
		// 		this.addChild(bt1);
		// 	}
		// }
		let bt1: egret.Bitmap = new egret.Bitmap();
		bt1.texture = RES.getRes('GameAssets_json.bgTile');
		bt1.width = this.stage.stageWidth;
		bt1.height = this.stage.stageHeight;
		bt1.fillMode = egret.BitmapFillMode.REPEAT;
		this.addChild(bt1);

		//绘制计时器区域
		let jx: egret.Shape = new egret.Shape();
		jx.graphics.beginFill(0x000000, 0.8);
		jx.graphics.drawRect(0, 0, 189, 100);
		jx.graphics.endFill();
		this.addChild(jx);

		//绘制计时器文本
		let NowTIME: egret.TextField = new egret.TextField();
		NowTIME.text = `${30}`;
		NowTIME.x = 0;
		NowTIME.y = 0;
		NowTIME.width = 189;
		NowTIME.height = 100;
		NowTIME.size = 40;
		NowTIME.textAlign = egret.HorizontalAlign.CENTER;
		NowTIME.verticalAlign = egret.VerticalAlign.MIDDLE;
		this.addChild(NowTIME);
		this.NowTIME = NowTIME;

		//绘制计时器区域
		let jx1: egret.Shape = new egret.Shape();
		jx1.graphics.beginFill(0x000000, 0.8);
		jx1.graphics.drawRect(189, 0, 189, 100);
		jx1.graphics.endFill();
		this.addChild(jx1);

		//绘制题目区域
		let jx2: egret.Shape = new egret.Shape();
		jx1.graphics.beginFill(0xffffff, 1);
		jx1.graphics.drawRect(189, 0, 2, 100);
		jx1.graphics.endFill();
		this.addChild(jx1);



		//创建计时器
		let jisiqi: egret.Timer = new egret.Timer(1000, 30);
		jisiqi.start();//开启定时器
		jisiqi.addEventListener(egret.TimerEvent.TIMER, this.myfunction, this);
		this.jisiqi = jisiqi;
		//length

		//获取数据
		let gg = await RES.getResAsync("nimeiAndOther_json");
		let gg1 = gg.arrCategoryName;
		this.gg1 = gg1;
		let suiji = Math.floor(Math.random() * gg1.length);//获取随机下标
		this.suiji = suiji;
		let shuju = gg1[suiji];
		this.shuju = shuju;
		//gg.mapCategoryName2ScreenName[shuju];
		//console.log(gg.mapCategoryName2ScreenName[shuju]);


		let gg2 = gg.mapCategoryName2ScreenName;
		this.gg2 = gg2;
		let Text1: egret.TextField = new egret.TextField();
		Text1.text = `${gg2[shuju]}`;
		Text1.x = 201;
		Text1.y = 0;
		Text1.width = 189;
		Text1.height = 100;
		Text1.size = 40;
		Text1.textAlign = egret.HorizontalAlign.CENTER;
		Text1.verticalAlign = egret.VerticalAlign.MIDDLE;
		this.addChild(Text1);
		this.Text1 = Text1;



		let a = this.myfunction1(gg.items);
		let b = this.myfunction1(gg.coords);
		for (var i = 0; i < gg.items.length; i++) {
			let bt2: egret.Bitmap = new egret.Bitmap();
			bt2.texture = RES.getRes(a[i]);
			//	let GJname=a[i];
			bt2.x = b[i][0];
			bt2.y = b[i][1];
			bt2.anchorOffsetX = bt2.width / 2;
			bt2.anchorOffsetY = bt2.height / 2;
			this.addChild(bt2);
			//	this.GJname=GJname;
			bt2.name = a[i];
			bt2.touchEnabled = true;
			this.bt2 = bt2;
			//	console.log(g1+g);
		}

		this.addEventListener(egret.TouchEvent.TOUCH_TAP, this.dj, this);



		let shezhi = this.shezhi("设置", this.stage.stageWidth - 100, 0, 100, 50, 0x000000, false);
		this.addChild(shezhi);
		shezhi.touchEnabled = true;
		shezhi.addEventListener(egret.TouchEvent.TOUCH_TAP, this.onshezhi, this);


	}
	private myfunction() {
		this.NowTIME.text = `${this.jisiqi.repeatCount - this.jisiqi.currentCount - this.num1}`;

		if ((this.jisiqi.currentCount + this.num1) >= 30) {
			let s3: Scene3 = new Scene3();
			SceneManager.getInstance().changeScene(s3);
			this.jisiqi.stop();
		}
	}
	//将数组打乱的函数
	private myfunction1(data) {
		//获取数组长度
		var arrlen = data.length;
		//创建数组 存放下标数
		var try1 = new Array();
		for (var i = 0; i < arrlen; i++) {
			try1[i] = i;
		}
		//创建数组 生成随机下标数
		var try2 = new Array();
		for (var i = 0; i < arrlen; i++) {
			try2[i] = try1.splice(Math.floor(Math.random() * try1.length), 1);
		}
		//创建数组，生成对应随机下标数的数组
		var try3 = new Array();
		for (var i = 0; i < arrlen; i++) {
			try3[i] = data[try2[i]];
		}
		return try3;
	}

	static num = 0;
	private num1 = 0;
	private dj(e: egret.TouchEvent) {
		//console.log(e.target.name);
		//console.log(e.target.name.indexOf(this.shuju));
		if (e.target.name.indexOf(this.shuju) > -1) {
			//console.log("加分");
			Scene2.num++;
			SoundManager.Instance.bom();
			egret.Tween.get(e.target).to({ scaleX: 1.5, scaleY: 1.5 }, 250).to({ scaleX: 0.1, scaleY: 0.1 }, 250).call(function () {
				//this.stage.removeChild(e.target);
			}, this);
			this.removeChild(e.target);

			this.gg1.splice(this.suiji, 1);
			this.suiji = Math.floor(Math.random() * this.gg1.length);

			this.shuju = this.gg1[this.suiji];
			this.Text1.text = `${this.gg2[this.shuju]}`;
			//	console.log(Scene2.num);
			egret.Tween.get(this.Text1).to({ scaleX: 1.5, scaleY: 1.5 }, 250).to({ scaleX: 1, scaleY: 1 }, 250).call(function () {
				//	this.removeChild(Tex);
			}, this);
		} else {
			//console.log("减分");
			SoundManager.Instance.bom1();
			this.num1 = this.num1 + 2;
			let Tex: egret.TextField = new egret.TextField();
			Tex.text = `-2`;
			Tex.size = 50;
			Tex.x = 50;
			Tex.y = 50;
			this.addChild(Tex);

			egret.Tween.get(Tex).to({ scaleX: 2, scaleY: 2 }, 250).to({ scaleX: 1, scaleY: 1, y: -50 }, 250).call(function () {
				this.removeChild(Tex);
			}, this);
			//	this.NowTIME.text=`${this.jisiqi.repeatCount-this.jisiqi.currentCount-2}`;

			// this.shuju=this.gg1[Math.floor(Math.random()*this.gg1.length)];
			// this.Text1.text=`${this.gg2[this.shuju]}`;
		}
		return Scene2.num;

	}

	protected onRemove() {
	}
	private onshezhi(e: egret.TouchEvent) {
		let sz: Shezhi = new Shezhi();
		sz.x = this.stage.stageWidth / 2 - sz.width / 2;
		sz.y = this.stage.stageHeight / 2 - sz.height / 2 - 100;
		SceneManager.getInstance().pushScene(sz);
		e.stopImmediatePropagation();
	}
}
